$(function() {
    let company_id = GetQueryString('company_id');
    const token = GetQueryString('token');
    $('.select_company').attr('data-company_id', company_id);

    $.ajax({
        type: "post",
        url: url,
        data: { 'method': "Company|company_detail", 'company_id': company_id, 'token': token },
        dataType: 'json',
        success: function(res) {
            // console.log(res);
            if (res.status == 1) {
                let data = res.data;
                // 分享
                let shareUrl = domain + "/view/share_companyDetail2.html?company_id=" + data.id;
                let shareTitle = data.company_name;
                let shareDes = data.description;
                let shareImgUrl = data.company_logo;

                // 分享
                $('.share').on('click', function() {
                    if (isAndroid_ios()) {
                        //安卓  
                        window.android.shareWebMethod(shareUrl, shareTitle, shareDes, shareImgUrl);
                    } else {
                        //ios
                        // window.webkit.messageHandlers.shareWebMethod.postMessage(shareUrl, shareTitle, shareDes, shareImgUrl);
                        var d = {};
                        d.shareUrl = shareUrl;
                        d.title = shareTitle;
                        d.description = shareDes;
                        d.imgUrl = shareImgUrl;
                        window.webkit.messageHandlers.shareWebMethod.postMessage(d);
                    }
                })

                // 客服
                $('.contactBtn').attr('data-uid', data.hx_uid);
                $('.hetitle').html(data.company_name);
                if (data.company_level == 0 || data.user_id == '') {
                    $('.contactService').hide();
                }
                $("#company_name").html(data.company_name);
                $("#company_level").attr("alt", data.company_level);
                if (data.level_logo != '') {
                    $("#company_level").attr("src", data.level_logo);
                } else {
                    $("#company_level").remove();
                }
                $("#company_logo").attr("src", data.company_logo);
                let phones = data.company_phones.split(',');
                let phoneHtml = '';
                phones.forEach(item => {
                    phoneHtml += `
                        <a href="tel:${item}" style="display:block;">手机：${item}</a>
                    `;
                })
                $("#company_phones").html(phoneHtml);
                $("#company_phones").attr("href", "tel:" + data.company_phones);
                $("#main_business").html(data.main_business == "" ? "暂无数据" : data.main_business);
                $("#shdetdt2").html(data.description == "" ? "暂无数据" : data.description);
                if ($("#shdetdt2").height() < 30) {
                    $(".shdetdt3").hide();
                }
                $("#read_count").html("浏览量 " + data.read_count);
                let addr = data.address.split(',');
                addr.splice(0, 1, data.area_name + addr[0]);
                let addrHtml = '';
                addr.forEach(item => {
                    addrHtml += `
                    <p>${item}</p>
                `;
                })
                $("#address").html(addrHtml);
                let has_follow = data.has_follow
                if (has_follow == 1) {
                    $("#shopnati").addClass("on")
                }
                let pic = res.data.pic;
                let html = '';
                for (var i = 0; i < pic.length; i++) {
                    var o = pic[i];
                    html += `
                        <div class="swiper-slide"><img src="${o.pic_url}" alt="${o.pic_name}" class="baimg plusBanner" /></div>
                    `;
                }
                $(".adBanner .swiper-wrapper").html(html);
                // banner轮播
                if (pic.length == 1) {
                    new Swiper('.adBanner', {
                        autoplay: 3000,
                        // pagination: '.swiper-pagination',
                        loop: false
                    })
                } else {
                    new Swiper('.adBanner', {
                        autoplay: 3000,
                        // pagination: '.swiper-pagination',
                        loop: true
                    })
                }

                $('.plusBanner').on('click', function() {
                    let idx = $(this).parent().index();
                    dialog('#plusBanner');
                    let html = '';
                    pic.forEach(item => {
                        html += `
                                <div class="swiper-slide">
                                    <img class="showImg" src="${item.pic_url}" alt="">
                                </div>
                            `;
                    })
                    $('.plusBannerSwiper .swiper-wrapper').html(html);
                    // banner轮播
                    let plusBanner = new Swiper('.plusBannerSwiper', {
                        // pagination: '.swiper-pagination',
                        paginationType: 'fraction'
                    })
                    plusBanner.slideTo(idx, false);
                })
            } else if (res.status == 2) {
                location.href = "fox://logout";
            } else {
                alert(res.message);
            }
        }
    })

    //收藏企业
    $("#shopnati").on('click', function() {
        let company_id = $('.select_company').attr('data-company_id');
        let _this = $(this);
        // // console.log(company_id);
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Company|add_fav', 'token': token, 'company_id': company_id },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    let data = res.data;
                    $(_this).attr('data-has_follow', data.has_follow);
                    companyMsg();
                    // alert(res.message);
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    })

    // 联系客服
    $('.contactBtn').on('click', function() {
        let uid = $(this).attr('data-uid');
        if (uid != '') {
            if (isAndroid_ios()) {
                //安卓  
                window.android.startChat(uid);
            } else {
                //ios
                window.webkit.messageHandlers.startChat.postMessage(uid);
            }
        } else {
            alert('暂无客服信息！！！')
        }
    })
})